package com.habdqn.easybuy.web.front.shop;

import com.habdqn.easybuy.entity.Shop;
import com.habdqn.easybuy.entity.User;
import com.habdqn.easybuy.service.ShopService;
import com.habdqn.easybuy.service.impl.ShopServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Xing.Jiejian
 * @date 2020/12/2
 */
@WebServlet(name = "ShopAddServlet",urlPatterns = "/shop/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strProductId = request.getParameter("productId");
        if (StringUtils.isNotBlank(strProductId)){
            Shop shop = new Shop();
            shop.setProductId(Integer.parseInt(strProductId));
            String strQuantity = request.getParameter("quantity");
            shop.setProductQuantity(Integer.parseInt(strQuantity));
            User user = (User) request.getSession().getAttribute("currentUser");
            shop.setUserId(user.getId());

            ShopService shopService = new ShopServiceImpl();
            // 添加该用户的购物车记录
            shopService.add(shop);
            response.sendRedirect(request.getContextPath()+"/shop");
        }
    }
}
